package com.ruoyi.gxs.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.gxs.domain.GxsSecond;
import com.ruoyi.gxs.service.IGxsSecondService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.gxs.mapper.GxsJionMapper;
import com.ruoyi.gxs.domain.GxsJion;
import com.ruoyi.gxs.service.IGxsJionService;

/**
 * 参加活动Service业务层处理
 * 
 * @author 高晓松
 * @date 2023-12-30
 */
@Service
public class GxsJionServiceImpl implements IGxsJionService 
{
    @Autowired
    private GxsJionMapper gxsJionMapper;

    @Autowired
    private IGxsSecondService gxsSecondService;

    /**
     * 查询参加活动
     * 
     * @param gxsJoinid 参加活动主键
     * @return 参加活动
     */
    @Override
    public GxsJion selectGxsJionByGxsJoinid(Long gxsJoinid)
    {
        return gxsJionMapper.selectGxsJionByGxsJoinid(gxsJoinid);
    }

    /**
     * 查询参加活动列表
     * 
     * @param gxsJion 参加活动
     * @return 参加活动
     */
    @Override
    public List<GxsJion> selectGxsJionList(GxsJion gxsJion)
    {
        
        return gxsJionMapper.selectGxsJionList(gxsJion);
    }

    /**
     * 新增参加活动
     * 
     * @param gxsJion 参加活动
     * @return 结果
     */
    @Override
    public int insertGxsJion(GxsJion gxsJion)
    {
        return gxsJionMapper.insertGxsJion(gxsJion);
    }

    /**
     * 修改参加活动
     * 
     * @param gxsJion 参加活动
     * @return 结果
     */
    @Override
    public int updateGxsJion(GxsJion gxsJion)
    {
        return gxsJionMapper.updateGxsJion(gxsJion);
    }

    /**
     * 批量删除参加活动
     * 
     * @param gxsJoinids 需要删除的参加活动主键
     * @return 结果
     */
    @Override
    public int deleteGxsJionByGxsJoinids(Long[] gxsJoinids)
    {
        return gxsJionMapper.deleteGxsJionByGxsJoinids(gxsJoinids);
    }

    /**
     * 删除参加活动信息
     * 
     * @param gxsJoinid 参加活动主键
     * @return 结果
     */
    @Override
    public int deleteGxsJionByGxsJoinid(Long gxsJoinid)
    {
        return gxsJionMapper.deleteGxsJionByGxsJoinid(gxsJoinid);
    }


    /**
     * 申请加分
     *
     * @param gxsJoinids 参加活动主键
     * @return 结果
     */

    @Override
    public int applyBonusByGxsJoinids(Long[] gxsJoinids) {
        int i = 0;
        for (Long gxsJoinid : gxsJoinids) {
            GxsJion gxsJion = gxsJionMapper.selectGxsJionByGxsJoinid(gxsJoinid);
            gxsJion.setGxsIfAwarded(1L);
            gxsJionMapper.updateGxsJion(gxsJion);
            i++;
        }
        return i;
    }


    /**
     * 批量加分
     *
     * @param gxsJoinids 参加活动主键
     * @return 结果
     */
    @Override
    public int activityBonusByGxsJoinids(Long[] gxsJoinids) {
        int i = 0;
        for (Long gxsJoinid : gxsJoinids) {
            GxsJion gxsJion = gxsJionMapper.selectGxsJionByGxsJoinid(gxsJoinid);

            gxsJion.setGxsIfAwarded(2L);
            gxsJionMapper.updateGxsJion(gxsJion);

            GxsSecond gxsSecond = gxsSecondService.selectGxsSecondByUserid(gxsJion.getUserId());
            Long gxsScoreOld = gxsSecond.getGxsScore();
            Long gxsScoreNew = gxsJion.getGxsScore();
            gxsSecond.setGxsScore(gxsScoreNew + gxsScoreOld);
            gxsSecondService.updateGxsSecond(gxsSecond);
            i++;
        }
        return i;
    }

    /**
     * 查询已申请加分列表
     *
     * @param gxsJion 参加活动
     * @return 参加活动
     */
    @Override
    public List<GxsJion> selectexamineList(GxsJion gxsJion) {
        List<GxsJion> list = gxsJionMapper.selectexamineList(gxsJion);
        return list;
    }

    @Override
    public int getApplyJion(Long gxsJoinid) {
        GxsJion gxsJion = gxsJionMapper.selectGxsJionByGxsJoinid(gxsJoinid);
        gxsJion.setGxsIfJion(1L);
        int i = gxsJionMapper.updateGxsJion(gxsJion);
        return i;
    }

}
